package keeto.com.vn.classified.ui;


import java.util.Vector;

import keeto.com.vn.classfied.model.Area;
import keeto.com.vn.classified.manager.IAManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class ProvincesActivity extends  Activity implements OnClickListener,
OnItemClickListener{
	public static final String EXTRA_CHANGE_PROVINCE = "Change_Provinces";
	public static final int ACTION_CHANGE_AREA = 100;

	private ImageButton btnBack;
	private TextView tvTitle;
	private ImageView btnSearch;

	private LayoutInflater mInflater = null;
	private ListView lvProvinces;
	private Vector<Area> mProviencesData = null;
	private ProvincesAdapter adapter = null;
	private Area mArea = null;
	private Intent intent; 

	public static void actionChangeCityLaunch(Activity context) {
		Intent i = new Intent(context, ProvincesActivity.class);
		context.startActivityForResult(i,ACTION_CHANGE_AREA);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.ia_provinces_view);
		intent = getIntent();
		mInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

		btnBack = (ImageButton)findViewById(R.id.btn_sub_title_back);
		tvTitle = (TextView)findViewById(R.id.tv_sub_title_text);
		btnSearch = (ImageView)findViewById(R.id.btn_sub_title_search_home);
		btnSearch.setVisibility(View.GONE);
		tvTitle.setText(getString(R.string.cities_button_provinces));
		lvProvinces = (ListView) this.findViewById(R.id.lv_provinces);

		btnBack.setOnClickListener(this);

		// get data
		mProviencesData = IAManager.getInstance(this).getAllArea();
		mArea =  IAManager.getInstance(this).getMyArea();
		adapter = new ProvincesAdapter(this,mArea.getRemoteId());
		lvProvinces.setAdapter(adapter);
		lvProvinces.setOnItemClickListener(this);
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_sub_title_back:
			this.finish();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Area selectedArea = mProviencesData.get(position);
		IAManager.getInstance(getApplicationContext()).saveMyArea(selectedArea.getRemoteId());
		setResult(RESULT_OK, intent);
		ProvincesActivity.this.finish();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}



	/**
	 * 
	 * @author hung
	 *Adapter for listview Area.
	 * - Set position to show current area. 
	 */
	class ProvincesAdapter extends BaseAdapter {

		private String AreaId;
		private Context ctx;



		public ProvincesAdapter(Context ctx,String areaId) {
			super();
			this.AreaId = areaId;
			this.ctx = ctx;
		}

		@Override
		public int getCount() {
			if (mProviencesData == null || mProviencesData.size() == 0) {
				return 0;
			} else {
				return mProviencesData.size();
			}
		}

		@Override
		public Object getItem(int position) {
			return mProviencesData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder ;
			if (mProviencesData == null || mProviencesData.size() == 0) {
				return null;
			}
			if (convertView == null){
				convertView = mInflater.inflate(R.layout.ia_category_item_view,parent, false);
				holder = new ViewHolder();
				holder.tvCityName = (TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			String cityName = mProviencesData.get(position).getName();
			holder.tvCityName.setText(cityName);
			if (AreaId.equals(mProviencesData.get(position).getRemoteId())){
				holder.tvCityName.setSelected(true);
			}else{
				holder.tvCityName.setSelected(false);
			}

			return convertView;
		}

	}

	class ViewHolder {
		private TextView tvCityName;
	}
}
